using System;
using System.Collections.Generic;
using System.Text;

using Presentation.Contracts;
using Task.Contracts;
using Task;
using DataAccess;
using Globals;
using System.Data;


namespace Presentation
	{
	/// <summary>
	/// Add Forum Presenter.
	/// </summary>
	public sealed class TopicsPresenter
		{
		#region Members

		/// <summary>
		/// Instance of view.
		/// </summary>
		private IForumView view;

		/// <summary>
		/// Simple galleryTask.
		/// </summary>
		private IForumTask task;

		#endregion Members

		#region Construction/Finalization

		/// <summary>
		/// Initializes a new instance of the <see cref="AddForumPresenter"/> class.
		/// </summary>
		/// <param name="view">The view.</param>
		public TopicsPresenter(IForumView view)
			{
			this.view = view;
			task = new ForumTask();
			}

		#endregion Construction/Finalization

		#region Public Methods

		public void AddTopic(string topic, string firstComment)
			{
			comment tmp = new comment();
			tmp.contents = topic;
			tmp.date = DateTime.Now;
			int id = task.AddComment(view.ForumID, view.LoggedUserID, tmp);
			if (!string.IsNullOrEmpty(firstComment))
				{
				tmp = new comment();
				tmp.contents = firstComment;
				tmp.date = DateTime.Now;
				task.AddComment(view.ForumID, view.LoggedUserID, tmp, id);
				}
			}

		/// <summary>
		/// Initializes view.
		/// </summary>
		/// 
		public void Initialize()
			{
			if (task.ForumOwnerId(view.ForumID) == view.LoggedUserID)
				{
				view.newTopicButtonVisible = true;
				view.deleteButtonVisible = true;
				}

			else if (task.IsSignedUser(view.ForumID, view.LoggedUserID))
				{
				view.unsignButtonVisible = true;
				view.newTopicButtonVisible = true;
				}
			else
				{
				view.newTopicButtonVisible = false;
				view.deleteButtonVisible = false;
				view.unsignButtonVisible = false;
				}


			List<comment> tmpList = task.TopicListByDate(view.ForumID);
			//List<comment> tmpList = task.GetComments(view.ForumID, view.CommentsStartIndex, 25);
			DataTable dt = new DataTable();
			dt.Columns.Add("content", typeof(string));
			dt.Columns.Add("user", typeof(string));
			dt.Columns.Add("date", typeof(string));
			dt.Columns.Add("commentsNo", typeof(string));
			dt.Columns.Add("lastCommentDate", typeof(string));
			dt.Columns.Add("lastCommentUser", typeof(string));
			dt.Columns.Add("idOwner", typeof(string));
			dt.Columns.Add("idLast", typeof(string));
			dt.Columns.Add("idParentComment", typeof(string));
			foreach (DataAccess.comment c in tmpList)
				{
				DataRow dr = dt.NewRow();
				dr[0] = c.contents.ToString();
				dr[1] = c.user.login.ToString();
				dr[6] = c.user.id.ToString();
				dr[2] = c.date_Date.ToString() + " " + c.date_Time.ToString();
				dr[3] = task.CountCommentsInTopic(view.ForumID, c.id);//c.commentChild.Count.ToString();
				dr[8] = c.id.ToString();
				comment tmp = task.NewestCommentInTopic(c.id);
				if (tmp != null)
					{
					dr[4] = tmp.date_Date.ToString() + " " + tmp.date_Time.ToString();
					dr[5] = tmp.user.login.ToString();
					dr[7] = tmp.user.id.ToString();
					}
				else
					{
					dr[4] = "";
					dr[5] = "";
					}
				dt.Rows.Add(dr);
				}
			view.ShowComments(dt);
			//view.ShowComments(tudatatable);
			//view.ShowComments(task.GetComments(3,1,3));
			}
		public void DeleteForum()
			{
			task.DeleteOwnerForum(view.LoggedUserID, view.ForumID);
			}

		public void unsignFromForum()
			{
			task.unSignFromForum(view.LoggedUserID, view.ForumID);
			}

		#endregion Public Methods
		#region Private Methods
		/// <summary>
		/// Adds the new forum.
		/// </summary>
		public void PageComments()
			{
			view.PageComments(25, task.GetCommentsCount(view.ForumID));
			}


		#endregion Private Methods
		}
	}
